<? 
include("includes/functions.php"); 

function linkCheck($url) {
	global $html;
	$linkInfo  = @parse_url( $url );
	$fp = @fsockopen( $linkInfo["host"], 80, $errno, $errstr, 10 );
	if ( !$fp ) {
		return false;
	} else {
		if( empty( $linkInfo["path"] ) ) {
	        // If it is empty it fills it
	        $linkInfo["path"] = "/";
	     }
	     $query = "";
	
	     // Checks if there is a query string in the url
	     if( isset( $linkInfo["query"] ) ) {
	          // If there is a query string it adds a ? to the front of it
	          $query = "?".$linkInfo["query"]."";
	     }
	
	     // Sets the headers to send
	     $out  = "HEAD ".$linkInfo["path"]."".$query." HTTP/1.0\r\n";
	     $out .= "Host: ".$linkInfo["host"]."\r\n";
	     $out .= "Connection: close \r\n" ;
	     $out .= "User-Agent: link_checker/1.1\r\n\r\n";
	
	     // writes the headers out
	     @fwrite( $fp, $out );
	     $html = '';
	
	     // Reads what gets sent back
	     while ( !@feof( $fp ) ) {
	          $html .= @fread( $fp, 8192 );
	     }
	     // Closes socket
	     fclose( $fp );
	 }    
	 return $html;	
}
function getstatuscode( $header ) {
	global $status;
  // Splits the headers into an array
  $headers = explode( "\r\n", $header );
  unset( $header );
  for( $i=0;isset( $headers[$i] );$i++ ) {

    // Checks if the header is the status header
    if( preg_match( "/HTTP\/[0-9A-Za-z +]/i" ,$headers[$i] ) ) {

      // If it is save the status
      $status = preg_replace( "/http\/[0-9]\.[0-9]/i","",$headers[$i] );
    }
  }
  return $status;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
	<title>New document</title>
<meta name="generator" content="TSW WebCoder">
</head>

<body>
<h1>Mass Link Checker</h1>
<?
$sql = "SELECT * from transLinks ORDER BY Title LIMIT 90,20";
$result = mysql_db_query($dbname,$sql);
while ($value = mysql_fetch_assoc($result)) {
	// Call link check function
	$url = $value[Link];
	linkCheck($url);
	if ($html) { getstatuscode($html); }
	echo "<a href=\"$url\" target=\"_blank\">$url</a><br>\n";
	echo $status."<hr>\n";
}
?>


</body>
</html>